
<template>
	<el-main>
		<el-row :gutter="15">
			<el-col :xl="6" :lg="6" :md="8" :sm="12" :xs="24" v-for="item in list" :key="item.id">
				<el-card class="task task-item" shadow="hover">
					<h2>{{item.description}}</h2>
					<ul>
						<li>
							<h4>执行类</h4>
							<p>{{item.jobClassName}}</p>
						</li>
						<li>
							<h4>定时规则</h4>
							<p>{{item.cronExpression}}</p>
						</li>
						<li>
							<h4>参数</h4>
							<p>{{item.parameter}}</p>
						</li>
					</ul>
					<div class="bottom">
						<div class="state">
							<el-tag v-if="item.status=='0'" size="mini">执行中</el-tag>
							<el-tag v-if="item.status=='-1'" size="mini" type="info">停用</el-tag>
						</div>
						<div class="handler">
							<el-popconfirm title="确定暂停执行吗？" @confirm="pause(item)" v-if="item.status == 0" >
								<template #reference>
									<el-button type="primary" icon="el-icon-close-bold" size="mini" circle v-auth="'system.quartz.pause'"></el-button>
								</template>
							</el-popconfirm>
							<el-popconfirm title="确定恢复执行吗？" @confirm="resume(item)" v-if="item.status == -1" >
								<template #reference>
									<el-button type="primary" icon="el-icon-caret-right" size="mini" circle v-auth="'system.quartz.resume'"></el-button>
								</template>
							</el-popconfirm>
							<el-dropdown trigger="click">
								<el-button type="primary" icon="el-icon-more" size="mini" circle plain></el-button>
								<template #dropdown>
									<el-dropdown-menu>
										<el-dropdown-item @click="edit(item)" v-auth="'system.quartz.edit'">编辑</el-dropdown-item>
										<el-dropdown-item @click="execute(item)" v-auth="'system.quartz.execute'">执行一次</el-dropdown-item>
										<el-dropdown-item @click="logs(item)" v-auth="'system.quartzLog.pageList'">日志</el-dropdown-item>
										<el-dropdown-item @click="del(item)" divided v-auth="'system.quartz.delete'">删除</el-dropdown-item>
									</el-dropdown-menu>
								</template>
							</el-dropdown>
						</div>
					</div>
				</el-card>
			</el-col>
			<el-col :xl="6" :lg="6" :md="8" :sm="12" :xs="24" v-auth="'system.quartz.add'">
				<el-card class="task task-add" shadow="none" @click="add">
					<el-icon><el-icon-plus /></el-icon>
					<p>添加计划任务</p>
				</el-card>
			</el-col>
		</el-row>
	</el-main>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>

	<el-drawer title="计划任务日志" v-model="dialog.logsVisible" :size="800" direction="rtl" destroy-on-close>
		<logs :jobId="ctaskId"></logs>
	</el-drawer>
</template>

<script>
	import saveDialog from './save'
	import logs from './logs'

	export default {
		name: 'task-manage',
		components: {
			saveDialog,
			logs
		},
		provide() {
			return {
				list: this.list
			}
		},
		data() {
			return {
        ctaskId:"",
				dialog: {
					save: false,
					logsVisible: false
				},
				list: [
				]
			}
		},
		mounted() {
			this.getList();
		},
		methods: {
			async getList(){
				var res = await this.$API.system.task.listAll.get();
				if(res.success){
					this.list = res.data;
				}
			},
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			edit(task){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(task)
				})
			},
			del(task){
				this.$confirm(`确认删除 ${task.description} 计划任务吗？`,'提示', {
					type: 'warning',
					confirmButtonText: '删除',
					confirmButtonClass: 'el-button--danger'
				}).then(async () => {
					var res = await this.$API.system.task.delete.delete(task.id);
					if(res.success){
						this.$message.success("删除成功")
						await  this.getList();
					}
				})
			},
			execute(task){
				this.$confirm(`确认执行一次 ${task.description} 计划任务吗？`,'提示', {
					confirmButtonText: '执行',
				}).then(async () => {
					var res = await this.$API.system.task.execute.post(task.id);
					if(res.success){
						this.$message.success("执行成功")
						await  this.getList();
					}
				}).catch(() => {
					return false
				})
			},
			async pause(task){
					var res = await this.$API.system.task.pause.post(task.id);
					if(res.success){
						this.$message.success("暂停成功")
						await  this.getList();
					}
			},
			async resume(task){
				var res = await this.$API.system.task.resume.post(task.id);
				if(res.success){
					this.$message.success("恢复成功")
					await  this.getList();
				}
			},
			logs(task){
				this.dialog.logsVisible = true
        this.ctaskId = task.id;
        console.log("taskId======  "+this.ctaskId)
			},
			run(task){
				this.$message.success(`已成功执行计划任务：${task.title}`)
			},
			//本地更新数据
			handleSuccess(){
				this.getList();
			}
		}
	}
</script>

<style scoped>
	.task {height: 250px;}
	.task-item h2 {font-size: 15px;color: #3c4a54;padding-bottom:15px;}
	.task-item li {list-style-type:none;margin-bottom: 10px;}
	.task-item li h4 {font-size: 12px;font-weight: normal;color: #999;}
	.task-item li p {margin-top: 5px;}
	.task-item .bottom {border-top: 1px solid #EBEEF5;text-align: right;padding-top:10px;display: flex;justify-content: space-between;align-items: center;}

	.task-add {display: flex;flex-direction: column;align-items: center;justify-content: center;text-align: center;cursor: pointer;color: #999;}
	.task-add:hover {color: #5A55E4;}
	.task-add i {font-size: 30px;}
	.task-add p {font-size: 12px;margin-top: 20px;}
</style>
